package com.dodayum.dubbo;

import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.registry.RegistryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author DoDayum
 * @Date 2024-01-13-20:34
 */
@Service
public class InitDubboRegistry {

    public ServiceConfig<RegistryService> serviceConfig;

    public InitDubboRegistry() {

        System.out.println("注册中心开始初始化");

        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("DoDayum-dubbo");

        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName("dubbo");
        protocol.setPort(8000);
        protocol.setThreads(100);

        serviceConfig = new ServiceConfig<>();
        serviceConfig.setApplication(applicationConfig);
        serviceConfig.setRegister(false);
        RegistryConfig registryConfig = new RegistryConfig(RegistryConfig.NO_AVAILABLE);
        registryConfig.setGroup("dubbo");
        serviceConfig.setRegistry(registryConfig);
        serviceConfig.setProtocol(protocol);
        serviceConfig.setInterface(RegistryService.class);
        serviceConfig.setRef(new MyDubboRegistryService());
        serviceConfig.setCallbacks(10000);
        serviceConfig.setOndisconnect("disconnect");
        serviceConfig.setTimeout(1000 * 60);

        // 方法级配置
        List<MethodConfig> methods = new ArrayList<>();
        MethodConfig method3 = new MethodConfig();
        method3.setName("subscribe");

        List<ArgumentConfig> args = new ArrayList<>();
        ArgumentConfig arg1 = new ArgumentConfig();
        arg1.setIndex(1);
        arg1.setCallback(true);
        args.add(arg1);
        method3.setArguments(args);

        methods.add(method3);

        MethodConfig method4 = new MethodConfig();
        method4.setName("unsubscribe");
        methods.add(method4);

        serviceConfig.setMethods(methods);
        serviceConfig.export();

    }
}
